// pages/npcNews/index.js
import {
    article,
    region
} from '../../api/apiData'
var pageNum1 = 1; //页码
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        type: 1,
        channelList: [],
        channel_id: '',
        loadMoreText: '加载中...', //底部文字
        list: [],
        show: false,
        regionList: [], //地区列表
        region_name: '全部地区',
        title: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getList(1)
        // this.getRegion()
    },
    // 获取地区列表
    getRegion() {
        region.query({}, res => {
            let list = [{
                id: '',
                name: '全部地区'
            }]
            if (res.code == 1) {
                this.setData({
                    regionList: list.concat(res.data)
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    // 获取列表
    getList(page) {
        var that = this
        article.lists({
            page: page,
            size: 10,
            channel_id: 2,
        }, res => {
            that.setData({
                total: res.data.total
            })
            if (res.data.list.length > 0) {
                that.setData({
                    list: that.data.list.concat(res.data.list) //合并数据
                })
                if (11 > res.data.list.length > 0) {
                    that.setData({
                        loadMoreText: '' //没有更多了
                    })
                }

            } else {
                if (page !== 1) {
                    that.setData({
                        loadMoreText: '暂无数据'
                    })
                } else {
                    that.setData({
                        list: [],
                        loadMoreText: '暂无数据'
                    })
                }
            }
            wx.stopPullDownRefresh();
        })
    },
    onChange(event) {
        this.setData({
            active: event.detail.index,
        })
        let id = this.data.channelList[event.detail.index].id

        if (event.detail.index == 1) {
            article.channel.query({
                parent_id: id
            }, res => {
                this.setData({
                    channelList2: res.data,
                    channel_id: res.data[0].id,
                    type: res.data[0].id
                })
                pageNum1 = 1
                this.setData({
                    list: [],
                });
                this.getList(1)
            })
        } else {
            this.setData({
                channel_id: id,
            })
            pageNum1 = 1
            this.setData({
                list: [],
            });
            this.getList(1)
        }
    },
    toTitAct(e) {
        this.setData({
            type: e.currentTarget.dataset.type,
            channel_id: e.currentTarget.dataset.type,
        })
        pageNum1 = 1
        this.setData({
            list: [],
        });
        this.getList(1)
    },
    toShow() {
        this.setData({
            show: true
        })
    },
    onConfirm(e) {
        this.setData({
            region_id: e.detail.value.id,
            region_name: e.detail.value.name,
            show: false
        })
        pageNum1 = 1
        this.setData({
            list: [],
        });
        this.getList(1)
    },
    onCancel() {
        this.setData({
            show: false
        })
    },
    getTitle(e) {
        this.setData({
            title: e.detail.value
        })
        pageNum1 = 1
        this.setData({
            list: [],
        });
        this.getList(1)
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        pageNum1 = 1
        this.setData({
            list: [],
        });
        this.getList(1)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        pageNum1 += 1;
        this.getList(pageNum1)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})